package com.bite.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.bite.order.config.ExceptionHandlerUtil;
import com.bite.order.model.OrderInfo;
import com.bite.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/{orderId}")
    @SentinelResource(value = "/{orderId}", blockHandlerClass = ExceptionHandlerUtil.class, blockHandler = "handleException")
    public OrderInfo getOrderById(@PathVariable("orderId") Integer orderId) {
        //另外一个资源
//        OrderInfo orderInfo = new OrderInfo();
//        try {
//            orderInfo = orderService.selectOrderById(orderId);
//            return orderInfo;
//        } catch (UndeclaredThrowableException e) {
//            log.error("获取订单详情发⽣异常, e:", e);
//            return orderInfo;
//
//        }
        return orderService.selectOrderById(orderId);
    }

    public OrderInfo handleException(Integer orderId, BlockException e) {
        System.out.println("进入处理异常的方法");
        if (e instanceof FlowException) {
            return new OrderInfo();
        }
        return new OrderInfo();
    }

    @RequestMapping("/write")
    public String write() {
        orderService.queryOrderInfo();
        return "写操作";
    }

    @RequestMapping("/read")
    public String read() {
        orderService.queryOrderInfo();
        return "读操作";
    }


}
